package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.Result;
import com.bwie.vo.BrandVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
* @createDate 2025-05-23 16:19:49
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{

    @Autowired
    TbBrandMapper tbBrandMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public Result getBrandPage(BrandVo brandVo) {
        Page<TbBrand> page = new Page<>(brandVo.getPageNum(), brandVo.getPageSize());

        LambdaQueryWrapper<TbBrand> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(brandVo.getKeyWord())){
            wrapper.like(TbBrand::getBrandName,brandVo.getKeyWord());
        }
        wrapper.orderByDesc(TbBrand::getCreateTime);

        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);
        return Result.ok().setData(tbBrandPage);
    }

    @Override
    public Result addBrand(TbBrand tbBrand) {
        TbBrand brand = tbBrandMapper.selectOne(
                new LambdaQueryWrapper<TbBrand>().eq(TbBrand::getBrandName, tbBrand.getBrandName())
        );
        if(brand != null){
            return Result.error().setMsg("该品牌已存在");
        }

        String token = request.getHeader("token");
        if(token == null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);

        tbBrand.setCreateTime(new Date());
        tbBrand.setCreateBy(nickName);

        tbBrandMapper.insert(tbBrand);

        return Result.ok();
    }

    @Override
    public Result updateBrandStatus(TbBrand tbBrand) {
        TbBrand brand = tbBrandMapper.selectById(tbBrand.getBrandId());
        if(brand == null){
            return Result.error().setMsg("品牌不存在");
        }
        brand.setBrandStatus(tbBrand.getBrandStatus());
        String token = request.getHeader("token");
        if(token == null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);
        brand.setUpdateBy(nickName);
        brand.setUpdateTime(new Date());
        tbBrandMapper.updateById(brand);

        return Result.ok();
    }

    @Override
    public Result getBrandList() {
        List<TbBrand> tbBrands = tbBrandMapper.selectList(null);
        return Result.ok().setData(tbBrands);
    }
}




